package com.wtwd.campus.receiver;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.annotation.MsgType;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.entity.MqGeneralReply;
import com.wtwd.campus.entity.MqMessage;
import com.wtwd.campus.h5_manager.entity.DeviceLog;
import com.wtwd.campus.h5_manager.service.DeviceLogService;
import com.wtwd.campus.utils.ReceiverUtils;
import com.wtwd.campus.vcom.common.Constant;
import com.wtwd.campus.vcom.entity.VcomRequestRecord;
import com.wtwd.campus.vcom.service.RequestRecordService;
import com.wtwd.campus.vcom.service.VcomDeviceInfoService;
import com.wtwd.campus.vcom.utils.VcomPushUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author: ldaoliang
 * @DateTime: 2022/1/10 10:24
 * @Description:
 */
@Service
@MsgType(value = Constants.VCOM_NOT_DISTURB)
public class VcomNotDisturbReceiver implements IReceiverHandler{

    @Autowired
    private VcomDeviceInfoService vcomDeviceInfoService;
    @Autowired
    private RequestRecordService requestRecordService;
    @Autowired
    private VcomPushUtils vcomPushUtils;
    @Autowired
    private ReceiverUtils receiverUtils;
    @Autowired
    private DeviceLogService deviceLogService;



    @Override
    public void handlerMessage(MqMessage mqMessage) {
        MqGeneralReply mqGeneralReply = (MqGeneralReply) mqMessage.getObject();
        String imei = mqGeneralReply.getImei();
        String commandNumber = mqGeneralReply.getCommandNumber();
        String deviceDate = (String)mqGeneralReply.getResult();
        // 判断数据库中的免打扰是否是这条应答，如果是，则进行下一步
        boolean b = vcomDeviceInfoService.dealNotDisturbReply(imei, commandNumber);
        if(b){
            Map<String,Object> map = requestRecordService.getRequestRecordByCondition(imei,commandNumber, Constant.COMMAND_TYPE_NOT_DISTURB);
            if(map != null){
                // 推送requestId结果
                vcomPushUtils.pushAndUpdateRequestStatus((int)map.get("id"),(String)map.get("request_id"),Constant.STATUS_COMPLETE,Constant.COMMAND_TYPE_NOT_DISTURB,imei);
                // 发送下一条指令
                VcomRequestRecord nextSendCommand = requestRecordService.getNextSendCommand(imei, (String)map.get("request_id"), Constant.COMMAND_TYPE_NOT_DISTURB);
                if(nextSendCommand != null){
                    logger.info("威科姆免打扰下一条发送指令：流水号={}",nextSendCommand.getCommandNumber());
                    vcomDeviceInfoService.setNotDisturbData(nextSendCommand);
                }
                String delKey = Constants.VCOM_NOT_DISTURB.concat("-").concat(commandNumber);
                receiverUtils.deleteCommandInRedis(imei, delKey);
                //存储设备接收日志
                DeviceLog deviceLog = new DeviceLog();
                deviceLog.setSendUser(Constants.DEVICE_USER);
                deviceLog.setImei(imei);
                deviceLog.setBigType(Constants.VCOM_NOT_DISTURB);
                JSONObject contant = new JSONObject();
                contant.put("设备号", imei);
                contant.put("设备时间",deviceDate);
                deviceLog.setCmd(contant.toJSONString());
                deviceLog.setSendTime(System.currentTimeMillis());
                deviceLog.setCommandNumber(commandNumber);
                deviceLog.setState(1);
                deviceLogService.addDeviceLog(deviceLog);
            }
        }
    }
}
